package com.yanqu.road.server.manager.activity.suitang.manager;


import com.yanqu.road.entity.activity.suitang.data.CrossSuiTangAutoSettingData;
import com.yanqu.road.entity.activity.suitang.data.CrossSuiTangCity;
import com.yanqu.road.entity.activity.suitang.data.CrossSuiTangUnionJoinData;
import com.yanqu.road.entity.activity.suitang.data.CrossSuiTangUserData;
import com.yanqu.road.entity.activity.suitang.entity.team.CrossSuiTangTeam;
import com.yanqu.road.entity.activity.suitang.entity.union.CrossSuiTangUnionJoinMember;
import com.yanqu.road.entity.activity.suitang.entity.user.CrossSuiTangUserBaseInfo;
import com.yanqu.road.entity.activity.suitang.enums.eSuiTangTeamStatus;
import com.yanqu.road.entity.player.CrossUserBaseInfo;
import com.yanqu.road.server.manager.npc.CrossNpcUserMgr;
import com.yanqu.road.utils.RandomHelper;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

public class CrossSuiTangNpcTestActivityMgr extends CrossBaseActivityMgr {

    protected static Logger logger = LogManager.getLogger(CrossSuiTangNpcTestActivityMgr.class.getName());

    private boolean isNpcMode = false;

    private RandomHelper randomHelper = new RandomHelper();

    private List<Long> npcIdList = new ArrayList<>();

    public void addNpc(long num) {
        if (!isNpcMode) {
            return;
        }
        for (int i = 0; i < num; i++) {
            CrossUserBaseInfo newNpc = CrossNpcUserMgr.createCrossNpcUserBaseInfo();
            // 随机一个商会
            Set<String> allUnion = activity.getUnionJoinDataMap().keySet();
            String unionId = allUnion.stream().skip((int) (allUnion.size() * Math.random())).findFirst().orElse(null);

//            unionId = "666-1b40c6d9-cbf5-4580-b158-a0688a37da21";
            CrossSuiTangUnionJoinData union = activity.getUnionJoinDataMap().get(unionId);
            if (union == null) {
                return;
            }
            CrossSuiTangUnionJoinMember joinMember = new CrossSuiTangUnionJoinMember(newNpc.getUserId(), 88800000L);
            newNpc.getUserBaseInfo().setUnionUid(union.getUnionUid());
            newNpc.getUserBaseInfo().setUnionName(union.getUnionBaseInfo().getUnionName());

            CrossSuiTangUserData userData = activity.getUserData(newNpc.getUserId());
            userData.setUserBaseInfo(new CrossSuiTangUserBaseInfo(newNpc.getUserBaseInfo().getTitleId(), 3));
            userData.setUserId(newNpc.getUserId());
            userData.setUnionUid(union.getUnionUid());
            userData.setPatronsTotalNum(randomHelper.next(50, 100));
            userData.setPatronsTotalAbility(randomHelper.next(9999999, 999999999));
            userData.setPatronsNSum(30);
            userData.setSoldierAddition(0);
            userData.setActivityId(activity.getConfig().getActivityId());
            userData.setGroupId(activity.getGroupId());
            union.getMemberMap().put(newNpc.getUserId(), joinMember);

            // 记录下npc
            npcIdList.add(newNpc.getUserId());

            activity.getRoundActivityMgr().initUserRoundData(newNpc.getUserId());
            activity.getBarracksActivityMgr().refreshTeam(userData);
        }
    }

    public void npcTask(int total, int index) {
        for (int i = 0; i < npcIdList.size(); i++) {
            if (i % total != index) {
                continue;
            }
            long npcId = npcIdList.get(i);

            CrossSuiTangUserData userData = activity.getUserData(npcId);
            if (userData == null) {
                continue;
            }
            CrossSuiTangAutoSettingData autoSettingData = userData.getAutoSettingData();

            if (!autoSettingData.isAutoSpeed()) {
                autoSettingData.setAutoSpeed(true);
                autoSettingData.setSpeedLimit(activity.getConfig().getParamConfig().getMoveAutoTimeList().get(randomHelper.next(0, activity.getConfig().getParamConfig().getMoveAutoTimeList().size())));
                autoSettingData.setIngotsTimes(999999999);
            }

            CrossSuiTangTeam team = userData.getTeam();
            if (team == null) {
                continue;
            }
            if (team.getUserId() == 0) {
                activity.getBarracksActivityMgr().refreshTeam(userData);
            }

            eSuiTangTeamStatus teamStatus = activity.getMapActivityMgr().getTeamStatus(team);
            if (teamStatus == eSuiTangTeamStatus.Free) {
                // 空闲
//                CrossSuiTangCity city = activity.getCityActivityMgr().getCity(npcId);
//                if (!city.getBattlefield().getAtkTeamMap().isEmpty() && activity.getCityActivityMgr().isHomeCity(city)) {
                    // 不操作
//                    team.setAutoMove(false);
//                } else {
                    // 先补一下兵力
                    userData.getBarracks().setReadyNum(9999999L);
                    activity.getBarracksActivityMgr().changeSoldier(team.getUserId(), team.getMaxSoldier());
                    int canMoveCity = activity.getMapActivityMgr().getCanMoveCity(team);
                    if (canMoveCity == 0) {
                        continue;
                    }

                    team.setAutoMove(true);
                    if (canMoveCity != team.getTargetCityId()) {
                        team.setTargetCityId(canMoveCity);
                    }
                    activity.getMapActivityMgr().moveToCity(team, canMoveCity);
//                }
            }
        }
    }

    public boolean isNpcMode() {
        return isNpcMode;
    }

    public void setNpcMode(boolean npcMode) {
        isNpcMode = npcMode;
    }

    public List<Long> getNpcIdList() {
        return npcIdList;
    }

    public boolean isNpc(long userId) {
        return npcIdList.contains(userId);
    }
}
